use chrono::{DateTime, Utc};
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "products")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: Uuid,
    pub sku: String,
    pub price: f64,
    pub compare_price: Option<f64>,
    pub cost_price: Option<f64>,
    pub track_inventory: bool,
    pub weight: Option<f64>,
    pub status: String, // "draft", "published", "archived"
    pub featured_image_url: Option<String>,
    pub published_at: Option<DateTime<Utc>>,
    pub category_id: Option<Uuid>,
    pub brand_id: Option<Uuid>,
    pub created_at: DateTime<Utc>,
    pub updated_at: DateTime<Utc>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
    #[sea_orm(has_many = "super::product_translation::Entity")]
    ProductTranslation,
    #[sea_orm(has_many = "super::product_resource::Entity")]
    ProductResource,
    #[sea_orm(
        belongs_to = "super::category::Entity",
        from = "Column::CategoryId",
        to = "super::category::Column::Id"
    )]
    Category,
    #[sea_orm(
        belongs_to = "super::brand::Entity",
        from = "Column::BrandId",
        to = "super::brand::Column::Id"
    )]
    Brand,
}

impl Related<super::product_translation::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::ProductTranslation.def()
    }
}

impl Related<super::product_resource::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::ProductResource.def()
    }
}

impl Related<super::category::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Category.def()
    }
}

impl Related<super::brand::Entity> for Entity {
    fn to() -> RelationDef {
        Relation::Brand.def()
    }
}

impl ActiveModelBehavior for ActiveModel {}